home *** CD-ROM | disk | FTP | other *** search
/ Aminet 37 / Aminet 37 (2000)(Schatztruhe)[!][Jun 2000].iso / Aminet / comm / bbs / cit_src_AD08.lha / popular.c < prev    next >
C/C++ Source or Header  |  1998-06-14  |  9KB  |  341 lines

  1. /*
  2.  *                              popular.c
  3.  *
  4.  * Rough popularity estimator for rooms.
  5.  */
  6.  
  7. #include "ctdl.h"
  8. #include <stdio.h>
  9. #include <string.h>
  10. #include <stdlib.h>
  11. #include <math.h>
  12. #include <ctype.h>
  13. #include <time.h>
  14. #include <proto/exec.h>
  15. #include <dos/dos.h>
  16. #include <pragmas/dos_pragmas.h>
  17. #include "exec/memory.h"
  18. #include "exec/ports.h"
  19. #include "exec/exec.h"
  20.  
  21. /*
  22.  *                              history
  23.  *
  24.  * 90Apr27 HAW  Version 2.3 - sorted output.
  25.  * 89Oct13 HAW  Version 2.2.
  26.  * 86May12 HAW  Version 2.1.
  27.  * 86Apr?? HAW  Version 2.0.
  28.  * 86Apr07 HAW  Version 1.1.
  29.  * 86Apr01 HAW  Created.
  30.  */
  31.  
  32. /*
  33.  *                              contents
  34.  *
  35.  *      crashout()              irrecoverable error
  36.  *      main()                  Main controller for this program
  37.  */
  38. int  mPrintf(char *format, ...) {return 0; }  /* stub to quiet the linker */
  39. FILE            *netLog=stdout;
  40.  
  41. #define LINE \
  42. "------------------------------------------------------------------------------"
  43.  
  44. struct counts {
  45.     int  forgot, num;
  46.     int  messages;
  47.     long bytes;
  48. } *countTab;
  49.  
  50. int    messFlag = FALSE;
  51. extern MessageBuffer  msgBuf; /* The -sole- message buffer      */
  52. extern FILE *msgfl, *msgfl2;
  53. extern FILE *upfd;  /* file descriptor for the msg file */
  54. int PublicRoomCount = 0, ForgetCount = 0;
  55. int activeLogs = 0, activeForgots = 0;
  56.  
  57. extern rTable *roomTab; /* RAM index of rooms   */
  58. extern aRoom  roomBuf;  /* room buffer  */
  59. extern FILE     *roomfl;        /* file descriptor for rooms    */
  60. extern int      thisRoom;       /* room currently in roomBuf    */
  61. extern logBuffer logBuf;        /* Log buffer of a person       */
  62. extern int      thisLog;        /* entry currently in logBuf    */
  63. extern FILE     *logfl; /* log file descriptor  */
  64. extern CONFIG    cfg;   /* A buncha variables   */
  65.  
  66. void getCUdate(int *year, char **month, int *day, int *hours, int *minutes);
  67. void flags(int argc, char *argv[]);
  68. int mulchAcct(void);
  69. void doMessages(void);
  70. void display(void);
  71. void handle(void);
  72.  
  73. /*
  74.  * crashout()
  75.  *
  76.  * This handles the irrecoverable error.
  77.  */
  78. void crashout(str)
  79. char *str;
  80. {
  81.     exit(printf(str));
  82. }
  83.  
  84. /*
  85.  * display()
  86.  *
  87.  * This function displays the results.
  88.  */
  89. void display()
  90. {
  91.     int rover;
  92.     void ShowIt();
  93.     int year, day, hours, minutes;
  94.     char *month;
  95.     int numcmp();
  96.     static SListBase Sorted = { NULL, NULL, numcmp, NoFree, NULL };
  97.  
  98.  
  99.     for (rover = 0;  rover < MAXROOMS;  rover++)
  100.         if (roomTab[rover].rtflags.INUSE != 0 &&
  101.             roomTab[rover].rtflags.PUBLIC != 0) {
  102.             AddData(&Sorted, countTab + rover, NULL, FALSE);
  103.             countTab[rover].num = rover;
  104.         }
  105.  
  106.     getCUdate(&year, &month, &day, &hours, &minutes);
  107.     printf("%d%s%02d @ %d:%02d\n\n", year, month, day, hours, minutes);
  108.  
  109.     printf(
  110. "Out of a log of %d entries, %d are in use; %d (%d%%) have used <Z>Forget.\n\n",
  111.  cfg.MAXLOGTAB, activeLogs, activeForgots, (100* activeForgots) / activeLogs);
  112.  
  113.     printf("%48s%15s\n", "Total", "Forgotten");
  114.     printf("%-25s%-15s%-13s%s", "Room name", "# Forgotten", "Percentage",
  115.                                                         "Percentage");
  116.     if (messFlag) printf("%10s", "Messages");
  117.     printf("\n%s\n", LINE);
  118.  
  119.     RunList(&Sorted, ShowIt);
  120.  
  121.     ForgetCount /= PublicRoomCount;
  122.     printf("%s\n\n", LINE);
  123.     printf("There are %d public rooms, ", PublicRoomCount);
  124.     printf("an average %d forgetting them (%d%% and %d%%)\n\n",
  125.                                                         ForgetCount,
  126.     (activeLogs != 0) ? (100 * ForgetCount) / activeLogs : 0,
  127.     (activeForgots != 0) ? (100 * ForgetCount) / activeForgots : 0);
  128. }
  129.  
  130. /*
  131.  * numcmp()
  132.  *
  133.  * This function will compare two counts and return who's higher.  This is
  134.  * used in list sorting.
  135.  */
  136. int numcmp(struct counts *s, struct counts *d)
  137. {
  138.     return d->forgot - s->forgot;
  139. }
  140.  
  141. /*
  142.  * ShowIt()
  143.  *
  144.  * This will show a room with forgotten stats.  This is used in list handling.
  145.  */
  146. void ShowIt(struct counts *s)
  147. {
  148.     PublicRoomCount++;
  149.     ForgetCount += s->forgot;
  150.     printf("%-30s%-15d%-9d%3d", roomTab[s->num].rtname,
  151.                                                         s->forgot,
  152.    (activeLogs != 0) ? (100 * s->forgot) / activeLogs : 0,
  153.    (activeForgots != 0) ? (100 * s->forgot) / activeForgots : 0);
  154.     if (messFlag) printf("%11d (%ld)", s->messages,
  155.                 (s->messages == 0) ? 0 : s->bytes / (long) s->messages);
  156.     printf("\n");
  157. }
  158.  
  159. /*
  160.  * doMessages()
  161.  *
  162.  * This function loops thru the msg file until finished.  It accumulates
  163.  * statistics, etc.
  164.  */
  165. void doMessages()
  166. {
  167.     MSG_NUMBER msg, firstMessage;
  168.     MSG_NUMBER total;   /* For stat keeping. */
  169.     extern struct mBuf mFile1;
  170.  
  171.     fprintf(stderr, "Mulching...\n");
  172.     InitMsgBase();
  173.     startAt(msgfl, &mFile1, 0, 0);
  174.     getMessage(getMsgChar, FALSE, TRUE, TRUE);
  175.     msg = atol(msgBuf.mbId);
  176.     fprintf(stderr, "%ld\n", msg);
  177.     firstMessage = msg;
  178.     handle();
  179.     getMessage(getMsgChar, FALSE, TRUE, TRUE);
  180.     msg = atol(msgBuf.mbId);
  181.     total = 1;
  182.     while (msg != firstMessage) {
  183.         total++;
  184.         fprintf(stderr, "%ld\r", msg);
  185.         handle();
  186.         getMessage(getMsgChar, FALSE, TRUE, TRUE);
  187.         msg = atol(msgBuf.mbId);
  188.     }
  189. }
  190.  
  191. /*
  192.  * flags()
  193.  *
  194.  * This function analyzes the command line arguments.  Very primitive.
  195.  */
  196. void flags(argc, argv)
  197. int argc;
  198. char *argv[];
  199. {
  200.     int i;
  201.  
  202.     for (i = 0; i < MAXROOMS; i++) {
  203.         countTab[i].forgot = 0;
  204.         countTab[i].messages = 0;
  205.     }
  206.  
  207.     while (argc != 1) {
  208.         argc--;
  209.         if (strCmpU("-M", argv[argc]) == 0)
  210.             messFlag = TRUE;
  211.     }
  212. }
  213.  
  214. /*
  215.  * getCUdate()
  216.  *
  217.  * This function retrieves the system date and returns in the parameters.
  218.  */
  219. void getCUdate(year, month, day, hours, minutes)
  220. int *year, *day, *hours, *minutes;
  221. char **month;
  222. {
  223.     int m;
  224.     static char *monthTab[13] = {"", "Jan", "Feb", "Mar",
  225.                                 "Apr", "May", "Jun",
  226.                                 "Jul", "Aug", "Sep",
  227.                                 "Oct", "Nov", "Dec" };
  228.  
  229.     getUtilDate(year, &m, day, hours, minutes);
  230.     *month = monthTab[m];
  231.     *year -= 1900;
  232. }
  233.  
  234. /*
  235.  * handle()
  236.  *
  237.  * This function searches the room base for the current msg's room, increments
  238.  * the appropriate counters when found.
  239.  */
  240. void handle()
  241. {
  242.     int rover;
  243.     int MsgLen(void);
  244.  
  245.     for (rover = 0; rover < MAXROOMS; rover++)
  246.         if (strCmpU(roomTab[rover].rtname, msgBuf.mbroom) == 0) {
  247.             countTab[rover].messages++;
  248.             countTab[rover].bytes += MsgLen();
  249.             break;
  250.         }
  251. }
  252.  
  253. /*
  254.  * MsgLen()
  255.  *
  256.  * This function figures out the byte usage of the message.
  257.  */
  258. int MsgLen()
  259. {
  260.     return  (int) (     strLen(msgBuf.mbtext)  + strLen(msgBuf.mbauth) +
  261.                     strLen(msgBuf.mbdate)  + strLen(msgBuf.mbtime) +
  262.                     strLen(msgBuf.mbId)    + strLen(msgBuf.mboname) +
  263.                     strLen(msgBuf.mborig)  + strLen(msgBuf.mbroom) +
  264.                     strLen(msgBuf.mbsrcId) + strLen(msgBuf.mbto) +
  265.                     strLen(msgBuf.mbaddr)  + strLen(msgBuf.mbOther) );
  266. }
  267.  
  268. /*
  269.  * main()
  270.  *
  271.  * This is the main controller.
  272.  */
  273. int main(int, char **);
  274. int main(argc, argv)
  275. int argc;
  276. char *argv[];
  277. {
  278.     SYS_FILE temp;
  279.     int Index;
  280.  
  281.     cfg.weAre = UTILITY;
  282.     printf("\nCitadel Room Popularity Estimator %s\n%s\n", VERSION_NAME, COPYRIGHT);
  283.     fprintf(stderr, "Munching...\n");
  284.     printf("\n");
  285.     if (readSysTab(FALSE, TRUE)) {
  286.         initRoomBuf(&roomBuf);
  287.         countTab = (struct counts *) GetDynamic(MAXROOMS * sizeof *countTab);
  288.         flags(argc, argv);
  289.         mvToHomeDisk(&cfg.homeArea);
  290.         makeSysName(temp, "ctdlroom.sys", &cfg.roomArea);
  291.         openFile(temp, &roomfl);
  292.         makeSysName(temp, "ctdllog.sys", &cfg.logArea);
  293.         openFile(temp, &logfl);
  294.         initLogBuf(&logBuf);
  295.  
  296.         for (Index = 0; Index < cfg.MAXLOGTAB; Index++) {
  297.             getLog(&logBuf, Index);
  298.             if (logBuf.lbflags.L_INUSE) {
  299.                 activeLogs++;
  300.                 activeForgots += mulchAcct();
  301.             }
  302.             fprintf(stderr, "%d\r", Index);
  303.         }
  304.  
  305.         if (messFlag)
  306.             doMessages();
  307.  
  308.         display();
  309.     }
  310.     return 0;
  311. }
  312.  
  313. /*
  314.  * mulchAcct()
  315.  *
  316.  * This goes through all the rooms, checking them against the current acct
  317.  * for statistics gathering.
  318.  */
  319. int mulchAcct()
  320. {
  321.     int i, j, g, usedForget;
  322.  
  323.     usedForget = 0;
  324.     for (i = 0;  i < MAXROOMS;  i++) {
  325.         if (roomTab[i].rtflags.PUBLIC != 0) {
  326.             if ((logBuf.lbgen[i] >> GENSHIFT) != roomTab[i].rtgen)  {
  327.                 j = roomTab[i].rtgen - (logBuf.lbgen[i] >> GENSHIFT);
  328.                 if (j < 0)
  329.                     g = -j;
  330.                 else
  331.                     g = j;
  332.                 if (g == FORGET_OFFSET) {
  333.                     usedForget = 1;
  334.                     countTab[i].forgot++;
  335.                 }
  336.             }
  337.         }
  338.     }
  339.     return usedForget;
  340. }
  341.